<?php
/**
 * 简单评论插件
 * @author ghoot
 */

namespace addons\Comment;
use app\common\controller\Addons;

class Comment extends Addons{

    public $info = array(
        'name'=>'Comment',
        'title'=>'评论插件',
        'description'=>'简单评论插件',
        'status'=>1,
        'author'=>'ghoot',
        'version'=>'1.0'
    );



    public function install(){
        $prefix = config('database.prefix');
        $sql="CREATE TABLE IF NOT EXISTS `" .$prefix. "comment` (
            `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
            `aim_table`  varchar(30) NULL  COMMENT '评论关联数据表',
            `aim_id`  int(10) NULL  COMMENT '评论关联ID',
            `ctime`  int(10) NULL  COMMENT '评论时间',
            `content`  text NULL  COMMENT '评论内容',
            `is_audit`  tinyint(2) NULL  DEFAULT 0 COMMENT '是否审核',
            `uid`  int(10) NULL  COMMENT 'uid',
            `uname`  varchar(30) NULL  COMMENT 'uname',
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci CHECKSUM=0 ROW_FORMAT=DYNAMIC DELAY_KEY_WRITE=0;";
    	$db = \think\Db::connect();
        $db->execute($sql);
        return true;
    }

    public function uninstall(){
        $prefix = config('database.prefix');
        $sql="DROP TABLE IF EXISTS `" .$prefix. "comment`;";
        $db = \think\Db::connect();
        $db->execute($sql);
    	return true;
    }

    public function comment($info){
        $config=$this->getConfig();
        if(empty($info) || empty($info['id']) ||  empty($info['model'])){ 
            //数据不正确
            return ;
        }
        $where = array('aim_table' => $info['model'],'aim_id' => $info['id']);
        $list = db('comment')->where($where)->select();
        $count = db('comment')->where($where)->count();
        $data = array('list'=>$list,'table'=>$info['model'],'id'=>$info['id'],'count'=>$count);
        $this->assign('data',$data);
        $this->template('comment');
    }
}